package edu.mobbuzz.page.menu.contact;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.DefaultListModel;
import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.plaf.Border;
import edu.mobbuzz.bean.Contact;
import edu.mobbuzz.facade.ContactFacade;
import edu.mobbuzz.page.menu.ContactMenu;
import edu.mobbuzz.page.menu.Menu;
import edu.mobbuzz.renderer.ContactRenderer;
import edu.mobbuzz.util.string.MenuString;
import edu.mobbuzz.util.string.ModeString;


public class ContactPage extends Menu {
    private Form contactForm;
    private Container contactPanel;
    private Label menuTitle;
    private DefaultListModel contactListModel;
    private List contactlist;
    private Command backCmd;
    private Command deleteCmd;
    private Command deleteAllCmd;
    private Command editCmd;
    private Command newCmd;
    private Command detailCmd;
    private ContactFacade contactFacade;
    
    public ContactPage(){   
        
        contactForm = new Form();
        contactPanel = new Container();
        menuTitle = new Label(getMenuTitle());
        contactListModel = new DefaultListModel(getContactFacade().displayContactList());
       
        detailCmd = new Command("Detail") {
            public void actionPerformed(ActionEvent evt) {
                contactDetail();
            }
        };
        
        newCmd = new Command("New Contact") {
            public void actionPerformed(ActionEvent evt) {
               editContact(ModeString.NEW);
            }
        };
        
        editCmd = new Command("Edit Contact") {
            public void actionPerformed(ActionEvent evt) {
                editContact(ModeString.EDIT);
            }
        };
        
        deleteCmd = new Command("Delete Contact") {
            public void actionPerformed(ActionEvent evt) {
                deleteContact();
            }
        };
        
        deleteAllCmd = new Command("Delete All Contact"){
            public void actionPerformed(ActionEvent evt) {
                deleteAllContact();
            }
        };


        backCmd = new Command("Back") {
            public void actionPerformed(ActionEvent evt) {
                    backToContactMenu();
            }
        };

        
    }
   
    public void showPage(){          
        menuTitle.setAlignment(Component.CENTER);
        menuTitle.getStyle().setMargin(5, 0, 5, 0);
        menuTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        menuTitle.getStyle().setBgTransparency(0);         
        
        contactPanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));      
                
        contactPanel.addComponent(menuTitle);
        contactPanel.addComponent(createList(contactListModel, List.VERTICAL, new ContactRenderer()));
        
        contactForm.setLayout(new BorderLayout());       
        contactForm.addComponent(BorderLayout.CENTER, contactPanel);  
        contactForm.addCommand(backCmd);

        
        if(contactListModel.getSize()!=0){
            contactForm.addCommand(deleteAllCmd);
            contactForm.addCommand(deleteCmd);
            contactForm.addCommand(editCmd);

        }

        contactForm.addCommand(newCmd);

        if(contactListModel.getSize()!=0){
            contactForm.addCommand(detailCmd);
        }
       
        
        
        contactForm.setCommandListener(getActionlistener());
        contactForm.setScrollableY(true);
        contactForm.show();
    }
    
    private List createList(DefaultListModel contactsListModel, int orientation, ListCellRenderer renderer) {
        contactlist = new List(contactsListModel){
            public void keyPressed(int keyCode){
               gameActionPerformed(keyCode);
            }
        };             
        contactlist.getStyle().setBgTransparency(0);
        contactlist.getStyle().setBorder(Border.getEmpty());
        contactlist.setListCellRenderer(renderer);
        contactlist.setOrientation(orientation);
        
        return contactlist;
    }
    
    public void gameActionPerformed(int keyCode){
        int game = Display.getInstance().getGameAction(keyCode);
        
        if(game == Display.GAME_FIRE){
            
            if(contactlist.size()==0){
                editContact(ModeString.NEW);
            }else{
                contactDetail();
            }
          
            
            
        }else if(game == Display.GAME_DOWN){            
            if(contactlist.getSelectedIndex()<(contactlist.size()-1)){
               contactlist.setSelectedIndex(contactlist.getSelectedIndex()+1);
            }else{
                contactlist.setSelectedIndex(0);
            }
           
        }else if(game == Display.GAME_UP){
             if(contactlist.getSelectedIndex()!=0){
                 contactlist.setSelectedIndex(contactlist.getSelectedIndex()-1);
             }else{
                 contactlist.setSelectedIndex(contactlist.size()-1);
             }
           
        }
    }
    
    
    public void contactDetail(){
        ContactDetail contactDetail = new ContactDetail();       
        contactDetail.setExitCmd(getExitCmd());
        contactDetail.setActionlistener(getActionlistener());
        contactDetail.setContact((Contact)contactlist.getSelectedItem());
        contactDetail.showPage();
    }

    public void editContact(int mode) {
        ContactForm editContactForm = new ContactForm();
        editContactForm.setMode(mode);
        editContactForm.setExitCmd(getExitCmd());
        editContactForm.setActionlistener(getActionlistener());
        if(mode==ModeString.EDIT){
            editContactForm.setContact(((Contact)contactlist.getSelectedItem()));
            editContactForm.setIndex(contactlist.getSelectedIndex());
        }
        editContactForm.showPage();
    }
    
    public void deleteContact(){
        TextArea body = new TextArea("Delete Contact ?", 2, 5);
        body.setGrowByContent(true);
        Command noCommand = new Command("No");
        Command yesCommand = new Command("Yes"){
            public void actionPerformed(ActionEvent evt) {
                getContactFacade().deleteContact(contactlist.getSelectedIndex());
                contactListModel.removeItem(contactlist.getSelectedIndex());
                if(contactListModel.getSize()==0){
                    contactForm.removeCommand(deleteCmd);
                    contactForm.removeCommand(deleteAllCmd);
                    contactForm.removeCommand(editCmd);
                    contactForm.removeCommand(detailCmd);
                }
                
                
            }
        };
        Dialog.show("", body, new Command[]{noCommand, yesCommand}, Dialog.TYPE_CONFIRMATION, null,0,null);
               
    }
    
    public void deleteAllContact(){
        TextArea body = new TextArea("Delete All Contact", 2, 5);
        body.setGrowByContent(true);
        Command noCommand = new Command("No");
        Command yesCommand = new Command("Yes"){
            public void actionPerformed(ActionEvent evt) {
                for(int i=0;i<contactListModel.getSize();i++){
                    getContactFacade().deleteContact(i);
                }
                               
                contactListModel.removeAll();
                if(contactListModel.getSize()==0){                    
                    contactForm.removeCommand(deleteCmd);
                    contactForm.removeCommand(deleteAllCmd);
                    contactForm.removeCommand(editCmd);
                    contactForm.removeCommand(detailCmd);
                }
                
                
            }
        };
        Dialog.show("", body, new Command[]{noCommand, yesCommand}, Dialog.TYPE_CONFIRMATION, null,0,null);

    }

    public void backToContactMenu(){        
        getContactFacade().closeContactRS();
        ContactMenu contactMenu = new ContactMenu();
        contactMenu.setExitCmd(getExitCmd());
        contactMenu.setActionlistener(getActionlistener());
        contactMenu.showPage();
      
        
    }
    
    public ContactFacade getContactFacade() {
        if(contactFacade==null)
            contactFacade = new ContactFacade();
        return contactFacade;
    }

    public String getMenuTitle() {
        return MenuString.CONTACT;
    }

    
}
